package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.PerssionCourseListAdapter1;
import com.github.postsanf.yinian.adapter.PerssionCourseListAdapter2;
import com.github.postsanf.yinian.bean.YNPerssion;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.widget.MySwipeMenuListView;
import com.github.postsanf.yinian.widget.SwipeMenu;
import com.github.postsanf.yinian.widget.SwipeMenuCreator;
import com.github.postsanf.yinian.widget.SwipeMenuItem;
import com.github.postsanf.yinian.widget.SwipeMenuListView;

import java.util.ArrayList;
import java.util.List;

public class StealEachCourse1Activity extends BaseActivity implements View.OnClickListener {
    private int userId;
    private List<YNPerssion> ynPerssionList = new ArrayList<>();
    private List<YNPerssion> ynPerssionList1 = new ArrayList<>();
    private List<YNPerssion> ynPerssionList2 = new ArrayList<>();
    private MySwipeMenuListView mySwipeMenuListView1;
    private MySwipeMenuListView mySwipeMenuListView2;
    private PerssionCourseListAdapter1 perssionListAdapter1;
    private PerssionCourseListAdapter2 perssionListAdapter2;
    private RelativeLayout rl_openeach, rl_writeyouopen, stealcourse1_top_cover, stealcourse2_top_cover, stealcourse1_buttom_cover, stealcourse2_buttom_cover;
    private View rl_openeach_kong, rl_writeyouopen_kong;

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    perssionListAdapter1.notifyDataSetChanged();
                    if (ynPerssionList1.size() > 0) {
                        rl_openeach.setVisibility(View.VISIBLE);
                        rl_openeach_kong.setVisibility(View.VISIBLE);
                    } else {
                        rl_openeach.setVisibility(View.GONE);
                        rl_openeach_kong.setVisibility(View.GONE);
                    }
                    break;
                case 2:
                    perssionListAdapter2.notifyDataSetChanged();
                    if (ynPerssionList2.size() > 0) {
                        rl_writeyouopen.setVisibility(View.VISIBLE);
                        rl_writeyouopen_kong.setVisibility(View.VISIBLE);
                    } else {
                        rl_writeyouopen.setVisibility(View.GONE);
                        rl_writeyouopen_kong.setVisibility(View.GONE);
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stealeachcourse1);
        registerBoradcastReceiver();
        initView();
        initPerssionList();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mListReceiver);
    }

    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionPerssionCourseListFinish);
        myIntentFilter.addAction(ACTIONs.actionupdatePerssionCourseList);
        registerReceiver(mListReceiver, myIntentFilter);
    }

    BroadcastReceiver mListReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionupdatePerssionCourseList:
                    initUpdateList1();
                    stealcourse1_top_cover.setVisibility(View.GONE);
                    stealcourse2_top_cover.setVisibility(View.VISIBLE);
                    stealcourse1_buttom_cover.setVisibility(View.GONE);
                    stealcourse2_buttom_cover.setVisibility(View.VISIBLE);
                    break;
                case ACTIONs.actionPerssionCourseListFinish:
                    finish();
                    break;
            }
        }
    };

    private void initView() {
        userId = application.getCurID();
        rl_openeach = (RelativeLayout) findViewById(R.id.rl_openeach);
        rl_openeach_kong = (View) findViewById(R.id.rl_openeach_kong);
        rl_writeyouopen = (RelativeLayout) findViewById(R.id.rl_writeyouopen);
        rl_writeyouopen_kong = (View) findViewById(R.id.rl_writeyouopen_kong);
        stealcourse1_top_cover = (RelativeLayout) findViewById(R.id.stealcourse1_top_cover);
        stealcourse1_buttom_cover = (RelativeLayout) findViewById(R.id.stealcourse1_buttom_cover);
        stealcourse2_top_cover = (RelativeLayout) findViewById(R.id.stealcourse2_top_cover);
        stealcourse2_buttom_cover = (RelativeLayout) findViewById(R.id.stealcourse2_buttom_cover);
        mySwipeMenuListView1 = (MySwipeMenuListView) findViewById(R.id.list1);
        mySwipeMenuListView2 = (MySwipeMenuListView) findViewById(R.id.list2);
        perssionListAdapter1 = new PerssionCourseListAdapter1(StealEachCourse1Activity.this, ynPerssionList1, application.getCurGid());
        perssionListAdapter2 = new PerssionCourseListAdapter2(StealEachCourse1Activity.this, ynPerssionList2, application.getCurID(), application.getCurGid());
        mySwipeMenuListView1.setAdapter(perssionListAdapter1);
        mySwipeMenuListView2.setAdapter(perssionListAdapter2);
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem openItem = new SwipeMenuItem(getApplicationContext());
                openItem.setBackground(new ColorDrawable(Color.rgb(250, 50,
                        50)));
                openItem.setWidth(UIHelper.dip2px(StealEachCourse1Activity.this, 102));
                openItem.setTitle("取消授权");
                openItem.setTitleSize(16);
                openItem.setTitleColor(Color.WHITE);
                menu.addMenuItem(openItem);
            }
        };
        mySwipeMenuListView1.setMenuCreator(creator);
        mySwipeMenuListView1.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                YNPerssion ynPerssion = ynPerssionList1.get(position);
                ynPerssion.setIsMeSee(0);
                initUpdateList1();
                Cover1Visible();
                return false;
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                break;
        }
    }

    private void initPerssionList() {
        YNPerssion ynPerssion = new YNPerssion();
        ynPerssion.setUnickname("小忆");
        ynPerssion.setUpic("http://o9im0tspz.bkt.clouddn.com/xiaoyiIcon.png");
        ynPerssion.setIsMeSee(0);
        ynPerssionList.add(ynPerssion);
        YNPerssion ynPerssion2 = new YNPerssion();
        ynPerssion2.setUnickname("小年");
        ynPerssion2.setUpic("http://o9im0tspz.bkt.clouddn.com/xiaonianIcon.png");
        ynPerssion2.setIsMeSee(0);
        ynPerssionList.add(ynPerssion2);
        initUpdateList1();
        Cover1Visible();
    }

    private void Cover1Visible() {
        stealcourse1_top_cover.setVisibility(View.VISIBLE);
        stealcourse2_top_cover.setVisibility(View.GONE);
        stealcourse1_buttom_cover.setVisibility(View.VISIBLE);
        stealcourse2_buttom_cover.setVisibility(View.GONE);
    }

    private void initUpdateList1() {
        ynPerssionList1.clear();
        ynPerssionList2.clear();
        for (YNPerssion ynPerssion : ynPerssionList) {
            if (ynPerssion.getIsMeSee() == 1) {
                ynPerssionList1.add(ynPerssion);
            } else if (ynPerssion.getIsMeSee() == 0) {
                ynPerssionList2.add(ynPerssion);
            }
        }
        myHandler.sendEmptyMessage(1);
        myHandler.sendEmptyMessage(2);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_HOME:
                return true;
            case KeyEvent.KEYCODE_BACK:
                return true;
            case KeyEvent.KEYCODE_CALL:
                return true;
            case KeyEvent.KEYCODE_SYM:
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                return true;
            case KeyEvent.KEYCODE_VOLUME_UP:
                return true;
            case KeyEvent.KEYCODE_STAR:
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
